#include "ble.h"

#include "ble.h"
#include "usart.h"
#include "string.h"
#include "stdio.h"
#include "flash.h"
#include "myradio.h"
#include "secret.h"

#define BLENAME           "SHENNONGSHUZI"
#define BLEOK             "OK"
#define BLEAT             "AT"
#define BLECONNECTED      "CONNECT OK"
#define BLEDISCONNECTED   "DISCONNECT"
#define SETBLENAME        "AT+NAME=SHENNONGSHUZI"

struct UART2Data uart2data;

static char NAME[] = "+NAME";
static char SCONF[] = "SCONF";
static char GCONF[] = "GCONF";


void uart2registerIdleCallback(void)
{
	HAL_UARTEx_ReceiveToIdle_IT(&huart2, uart2data.uartBuf, UART2BUFLEN);
}

void uart2IdleCallback(uint16_t size)
{
	if(uart2data.received != 1) {
		uart2data.received = 1;
		memcpy(uart2data.buf, uart2data.uartBuf, size);
		//uart2registerIdleCallback();
		HAL_UARTEx_ReceiveToIdle_IT(&huart2, uart2data.uartBuf, UART2BUFLEN);
	} else {
		//wait unhandled data to check.
	}
}

void uart2DataHandle(void)
{
	//printf("uart flag: %d\r\n", uart2data.received);
	if(uart2data.received == 1) {
		printf(" uart2DataHandle data:[%s]\r\n", uart2data.buf);
		if(strncasecmp((char *)uart2data.buf, (char *)BLEOK, 2) == 0) {
			printf("BLE OK..\r\n");
		}else if(strncasecmp((char *)uart2data.buf, (char *)BLEAT, 2) == 0) {
			printf("BLE AT..\r\n");
			printf("data:[%s]\r\n", uart2data.buf);
		}else if(strncasecmp((char *)uart2data.buf, (char *)BLECONNECTED, sizeof(BLECONNECTED)-1) == 0) {
			printf("BLE connected..\r\n");	
			uart2data.connected = 1;
		}else if(strncasecmp((char *)uart2data.buf, (char *)BLEDISCONNECTED, sizeof(BLEDISCONNECTED)-1) == 0) {
			printf("BLE disconnected..\r\n");		
			//struct FlashData *conf = GET_FLASH_DATA();
			uart2data.connected = 0;
		}else if(strstr((char *)uart2data.buf, SCONF) !=NULL)  {
			char buf[128] = {0};
			char server[32] = {0};
			uint8_t secret[17] = {0};
			uint8_t freq  = 0;
			uint8_t power = 0;
			uint8_t net   = 0;
			uint8_t baud  = 0;
			uint8_t sf = 9;
			uint8_t cr = 1;
			char *fmt = "SCONF %s %d %d %d %d %d %d SCONF";
			if(sscanf((const char *)uart2data.buf, fmt, server, &freq, &net, &baud, &power, &sf, &cr) > 0) {
				printf("server: [%s] freq: %d net: %d, power: %d\r\n", server, freq, net, power);
				generatePassword(server, secret);
				printf("secret: %s\r\n", secret);
				int ret = FlashSetLoraConf(server, freq, net, 0, power, secret, sf, cr);
				struct FlashData *conf = GET_FLASH_DATA();
				LoraInit(conf->freq, conf->net, conf->baud, conf->power, conf->sf, conf->cr);
			int len = snprintf(buf, sizeof(buf), "{\"c\":\"%s\", \"t\":1,\"f\":%d, \"n\":%d, \"b\":%d,\"p\":%d,\"s\":%d,\"cr\":%d}", conf->self, conf->freq, conf->net, conf->baud, conf->power, conf->sf, conf->cr);
				HAL_UART_Transmit(&huart2, (uint8_t *)buf, len, HAL_MAX_DELAY);
				printf("send conf 1...\r\n");
			}
		}else if(strstr((char *)uart2data.buf, GCONF) != NULL) {
			struct FlashData *conf = GET_FLASH_DATA();
			char buf[32] = {0};
			char self[17] = {0};
			GetUID(self);
			int len = snprintf(buf, sizeof(buf), "{\"c\":\"%s\", \"t\":1}", self);
			if(len>0) {
				HAL_UART_Transmit(&huart2, (uint8_t *)buf, len, HAL_MAX_DELAY);
			}else {
				printf("WTF HAL_UART_Transmit...\r\n");
			}
		} else {
			//printf("WTF...\r\n");
		}
		
		uart2data.received = 0;
		memset(uart2data.buf, 0, sizeof(uart2data.buf));
	} else {
		//printf("flag is not set...\r\n");
	}
}

void BleInit(void)
{
	char getName[] = "AT+NAME?";
	char buf[32] = {0};
	uint8_t skip = 0;
	int counter = 0;
	while(1) {
		HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
		//printf("%s\r\n", getName);
		if(skip == 0) {
			HAL_StatusTypeDef status = HAL_UART_Transmit(&huart2, (uint8_t *)getName, strlen(getName), 0xFF); 
			printf("Transmit huart2 [%s]\r\n", getName);
			HAL_Delay(500);
		}
		if(uart2data.received == 1) {
			if(strncasecmp((char *)uart2data.buf, (char *)BLEOK, 2) == 0) {
				printf("BLE SET OK..\r\n");
				break;
			}else if (strstr((char *)uart2data.buf, (char *)NAME)!= NULL) {  //Handle ble name
				printf("strstr data:[%s]\r\n", uart2data.buf);
				if(sscanf((const char *)uart2data.buf, "%*10c+NAME:%s", buf) > 0) {
					printf("BLE NAME: [%s] name:[%s]\r\n", buf, BLENAME);
					if(strcmp(buf, BLENAME) != 0) {
						HAL_UART_Transmit(&huart2, (uint8_t *)SETBLENAME, sizeof(SETBLENAME)-1, 0xFF);
						HAL_Delay(500);
						skip = 1;
						printf("send setname: [%s] ...\r\n", SETBLENAME);
					} else {
						printf("BLE NAME OK [%s]..\r\n", buf);
						break;
					}
				}
			} else {
				skip = 0;
			}
			
			printf("data[%s] .........\r\n", uart2data.buf);
			uart2data.received = 0;
			memset(uart2data.buf, 0, sizeof(uart2data.buf));
		} else {
			skip = 0;
			printf("not data...\r\n");
		}
		
		if(skip ==0) {
				HAL_Delay(500);
		}
		
		counter = counter + 1;
		if (counter > 10) {
			HAL_NVIC_SystemReset();
		}
	}
	
	printf("ble init OK \r\n");
}
